package com.zjy.software.day14;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

public class ThreadDemo {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        //第一步 创建一个实现Runnable接口的对象
       Runnable runnable=new MyRunnableImple();

       //第二步 把创建的实现Runnable接口的对象 交给Thread去处理,通过构造器传入
        Thread thead=new Thread(runnable);

        //第三步 启动线程,staart是唯一启动多线程的方法,并且当前线程对象只能启动一次
        thead.start();
        System.out.println("thead = " + thead.getName());

        //线程的第二种实现方式,通过继承Thread类来实现
        Thread thread=new MyThread();
        thread.start();
        System.out.println("thread = " + thread.getName());

        //线程实现的第三种方式,使用Callable接口实现,并且有返回值
        //第一步 创建一个实现Callable接口的对象
        Callable<Integer> callable=new MyCallableImpl();

        //第二步 把创建的实现Callable接口的对象 交给featureTask对象
       FutureTask<Integer> futureTask=new FutureTask<>(callable);

       //第三步 把futuretask对象传入到Thread对象中,
        Thread futureThread=new Thread(futureTask);

        //第四步启动线程
        futureThread.start();


        //第五步,得到线程的返回值
        Integer i = futureTask.get();
        System.out.println("i = " + i);
        System.out.println("futureThread = " + futureThread.getName());

        for (int j = 0; j < 100; j++) {
            System.out.println("$$$$$$$$$$$$$$$$$$$$$$"+j);
        }

        Thread.sleep(3000);
        System.out.println("主线程结束");
    }
}
